import { IGood, CartItemModel } from "./declare";
import { cartModel } from "./CartModel";
import { action } from "mobx";

/**
 * 购物车消费者
 */


 export class CartModelCustomer {
     goodsMap: Map<string, IGood>

     constructor(){
         this.goodsMap = new Map()
         cartModel.hooks.push((cart: CartItemModel)=>{
           this.setGoodNumber(cart.Good.id, cart.number)
        })
     }

     
    @action
    setGoodNumber(id: number, num){
        const good = this.goodsMap.get(`${id}`)
        if(good){
            good.num = num
        }
    }

    getGoodNum(id: number){
        const cart = cartModel.items.find(it=> it.Good.id === id)
        return cart ? cart.number : 0
    }
 }